package com.kensure.shike.baobei.service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKBaobeiSkuDao;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKBaobeiSku;
import com.kensure.shike.baobei.model.SKBaobeiYansePic;

/**
 * 宝贝信息sku服务实现类
 * 
 * @author fankd created on 2019-6-10
 * @since
 */
@Service
public class SKBaobeiSkuService extends JSBaseService {

	@Resource
	private SKBaobeiSkuDao dao;
	@Resource
	private BaseKeyService baseKeyService;

	public SKBaobeiSku selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKBaobeiSku> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKBaobeiSku> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKBaobeiSku obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_baobei_sku"));
		obj.setNum(0);
		obj.setYushou(0);
		return dao.insert(obj);
	}

	public boolean update(SKBaobeiSku obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		params.put("updatedTime", new Date());
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 获取宝贝的SKU列表
	 * 
	 * @param bbid
	 * @return
	 */
	public List<SKBaobeiSku> getList(Long bbid) {
		Map<String, Object> parameters = MapUtils.genMap("bbid", bbid, "status", 9);
		List<SKBaobeiSku> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 获取宝贝总库存
	 */
	public Long sumNum(Long bbid) {
		Map<String, Object> parameters = MapUtils.genMap("bbid", bbid, "status", 9);
		Long sumnum = dao.sumByWhere(parameters);
		if (sumnum == null) {
			sumnum = 0L;
		}
		return sumnum;
	}

	/**
	 * 获取宝贝的SKU列表
	 * 
	 * @param bbid
	 * @return
	 */
	public SKBaobeiSku getSku(Long bbid, String yanse, String chima) {
		Map<String, Object> parameters = MapUtils.genMap("bbid", bbid, "yanse", yanse, "chima", chima);
		List<SKBaobeiSku> list = selectByWhere(parameters);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	public void add(SKBaobei baobei) {
		
	}

	/**
	 * 修改库存
	 */
	public void updateKuCun(List<SKBaobeiSku> skus, Long bbid) {
		for (SKBaobeiSku temp : skus) {
			SKBaobeiSku sku = getSku(bbid, temp.getYanse(), temp.getChima());
			sku.setNum(temp.getNum());
			sku.setYushou(temp.getYushou());
			update(sku);
		}
	}

	/**
	 * 减少库存数量
	 */
	public synchronized void subNum(Long skuid, Integer num) {
		SKBaobeiSku sku = selectOne(skuid);
		if (sku.getNum().compareTo(num) < 0) {
			BusinessExceptionUtil.threwException("该商品库存不足！");
		}
		updateNum(skuid, -num, 0);
	}

	/**
	 * 减少预约数量
	 */
	public synchronized void subYuyue(Long skuid, Integer yuyue) {
		updateNum(skuid, 0, -yuyue);
	}

	/**
	 * 增加库存数量
	 */
	public synchronized void addNum(Long skuid, Integer num) {
		updateNum(skuid, num, 0);
	}

	/**
	 * 增加预约数量
	 */
	public synchronized void addYuyue(Long skuid, Integer yuyue) {
		updateNum(skuid, 0, yuyue);
	}

	/**
	 * 修改num和yuyue
	 */
	private void updateNum(Long skuid, Integer numAdd, Integer yuyueAdd) {
		Map<String, Object> params = MapUtils.genMap("id", skuid, "numAdd", numAdd, "yuyueAdd", yuyueAdd);
		updateByMap(params);
	}

}
